//
//  CalclatorApp.m
//  iCalculator
//
//  Created by Stephen Houser on 9/7/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//
#import "CalculatorApp.h"
#import "CalculatorEngine.h"
#import "BasicKeypad.h"

// This needs to be moved to a preferences list or something "very nice" like that.
// I dont like long-term hardcoded things. But, hey, I'm not an Objective-C god 
// so cut me some slack.
#define DISPLAY_HTML @"<span style=\"color:#124564;text-align:right;display:block;font-family:helvetica;font-size:64px;font-weight:normal;width:100%;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;letter-spacing:-2px;\">%@</span>"

@implementation CalculatorApp

/*
 * Take some unformatted text (perhaps a number) and display it on the
 * main "results" area of the calculator.
 */
- (NSString *) setDisplay: (NSString *) newDisplay {
	if (displayHTML != Nil) {
		[displayHTML release];
	}
	
	// fancy HTML formatted text that is...
	displayHTML = [[NSString alloc] initWithFormat: DISPLAY_HTML, newDisplay ];
	//NSLog(@"HTML = %@", displayHTML);
	
	[resultDisplay setHTML: displayHTML];
	
	// telling the resultDisplay was not getting it refreshed liked
	// I expected. But refreshing the whole view does... Hummm...
	[resultDisplay setNeedsDisplay];
	[basicCalculatorView setNeedsDisplay];
	
	return newDisplay;
}

/*
 * This is the command interface. All the keypressed go through here.
 * This routine then farms out to the models (engine) and other views
 * when/if needed to ensure everyone sees what is going on.
 */
- (void) sendCommand: (NSString *) command {
	//NSLog(@"CalculatorApp sendCommand: [%@]", command);
	
	// pass the keypress/command to the calculator engine "model"
	[engine sendCommand: command];
	
	// now update the "views"
	[self setDisplay: [engine getDisplay]];
}

/*
 * Main entry point for the UIKit application. This gets called
 * to "start" the application from all that jazz in main.
 *
 * Herein the main windows and views are created and their user 
 * interfaced built. Then, blamo! it all gets put up on the screen
 * and the event loop takes over. Just like all good event driven 
 * applications. It's like magic.
 */
- (void) applicationDidFinishLaunching: (id) fp8 {

	// create the calculation engine, we need this later -- it does all the work.
	engine = [[CalculatorEngine alloc] init];

	// init main window for the application to be the whole screen.
	struct CGRect frame = [UIHardware fullScreenApplicationContentRect];
	//NSLog(@"frame is %f, %f, %f, %f", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
	mainWindow = [[UIWindow alloc] initWithContentRect: frame];

	// init main view that goes in the main window, again, the whole screen
	frame.origin.x = frame.origin.y = 0.0f;

	// The "master" view that contains a stack of views that you can
	// "slide" between using UITransitionView. We just start with the
	// calculator view for now, until I get the sliding working.
	masterView = [[UITransitionView alloc] initWithFrame: frame];

	// The basic calculator view -- similar to Apple's -- goes in the masterView
	basicCalculatorView = [[UIView alloc] initWithFrame: frame];
	
	// The result text is just 89 pixels at the top.
	// hardcoded for now.
	struct CGRect resultRect = frame;
	resultRect.size.height = 89.0f;
	//NSLog(@"resultRect is %f, %f, %f, %f", resultRect.origin.x, resultRect.origin.y, resultRect.size.width, resultRect.size.height);

	// result text field up there at the top -- like a normal calculator
	resultDisplay = [[UITextView alloc] initWithFrame: resultRect];
	[resultDisplay setEditable: NO];
	[resultDisplay setScrollingEnabled: NO];
	//[resultText setTextSize: 64];
	[basicCalculatorView addSubview: resultDisplay]; 
	
	// The keypad is below the results, 90 pixels, again hardcoded for now.
	struct CGRect keypadRect = frame;
	keypadRect.origin.y = resultRect.size.height;
	
	basicKeypad = [[BasicKeypad alloc] initWithFrame: keypadRect];
	[basicKeypad setImage: [UIImage imageNamed: @"CalculatorBackground.png"]];
	[basicKeypad setController: self];
	[basicCalculatorView addSubview: basicKeypad];

	// A simple sample navigation bar -- not "really" used just yet.
	navView = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];
	[navView showButtonsWithLeftTitle: @"Decimal" rightTitle: @"Hex" leftBack: YES];
	[navView setPrompt: @"Caclulator2"];
	[navView setBarStyle: 0];
	[navView enableAnimation];

	// A diagnostic view -- not "really" used just yet. 
	diagView = [[UITouchDiagnosticsLayer alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
	[diagView setDisplayChargeMap: true];
	[diagView setDisplayHitRects: true];
	[diagView setDisplayTouchEvents: true];
	[diagView setTrackTouchEvents: true];
	
	// These first two do work, but why waste the memory right now.
	//[masterView addSubview: navView];
	//[masterView addSubview: diagView];
	[masterView addSubview: basicCalculatorView];
	
	[mainWindow setContentView: masterView]; 
	[mainWindow orderFront: self];
	[mainWindow _setHidden: NO];	

	// This is how to slide.... for later
	//[masterView transition:1 toView: basicCalculatorView];	
}

@end
